package com.yunxi.service.dubboService.impl.finance;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.finance.pojo.*;
import com.yunxi.finance.pojo.DownPaymentAmountComputeReqVO;
import com.yunxi.finance.pojo.LeaseContractAmountComputeReqVO;
import com.yunxi.finance.pojo.MarginAmountComputeReqVO;
import com.yunxi.finance.service.FinanceComputeDubboService;
import com.yunxi.finance.service.VehiclePriceDubboService;
import com.yunxi.model.finance.FinancialComputeColumns;
import com.yunxi.service.localService.finance.FinancialComputeColumnsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

public class FinanceComputeDubboServiceImpl implements FinanceComputeDubboService {

    @Autowired
    private FinancialComputeColumnsService financialComputeColumnsServiceImpl;

    @Autowired
    private VehiclePriceDubboService vehiclePriceDubboService;

    /**
     * 金融计算器项目分页查询
     *
     * @param queryVO 查询条件
     * @return 查询结果
     */
    @Override
    public ApiResult page(FinancialComputeColumnsReqVO queryVO, Integer pageNum, Integer pageSize) {
        return ResponseUtil.ok(financialComputeColumnsServiceImpl.pageByVo(queryVO, pageNum, pageSize));
    }

    /**
     * 金融计算器项目不分页查询
     *
     * @param queryVO 查询条件
     * @return 查询结果
     */
    @Override
    public ApiResult list(FinancialComputeColumnsReqVO queryVO) {
        return ResponseUtil.ok(financialComputeColumnsServiceImpl.listByVo(queryVO));
    }

    /**
     * load by id
     *
     * @param id id
     * @return 查询结果
     */
    @Override
    public ApiResult load(String id) {
        return ResponseUtil.ok(financialComputeColumnsServiceImpl.getById(id));
    }

    /**
     * 保存
     *
     * @param editVO editVO
     * @return ApiResult
     */
    @Override
    public ApiResult save(FinancialComputeColumnsEditVO editVO) {
        FinancialComputeColumns entity = new FinancialComputeColumns();
        BeanUtils.copyProperties(editVO, entity);
        financialComputeColumnsServiceImpl.saveOrUpdate(entity);
        return ResponseUtil.ok();
    }

    /**
     * 计算融资租赁合同金额 成交价格 + 附件包价格 + GPS价格
     */
    @Override
    public BigDecimal computeLeaseContractAmount(LeaseContractAmountComputeReqVO vo) {
        return vo.getTransPrice().add(vo.getAnnexPack()).add(vo.getGpsPrice());
    }

    /**
     * 计算首付款金额 融资租赁合同金额 * 首付款比例 = 首付款金额
     */
    @Override
    public BigDecimal computeDownPaymentAmount(DownPaymentAmountComputeReqVO vo) {
        return vo.getLeaseContractAmount().multiply(vo.getDownPaymentRaio());
    }

    /**
     * 计算保证金金额 融资租赁合同金额 * 保证金比例 = 保证金金额
     */
    @Override
    public BigDecimal computeMarginAmount(MarginAmountComputeReqVO vo) {
        if (vo.getLeaseContractAmount() == null || vo.getMarginRatio() == null) {
            return BigDecimal.valueOf(0);
        }
        return vo.getLeaseContractAmount().multiply(vo.getMarginRatio());
    }

    /**
     * 计算留够价金额(尾款) 融资租赁合同金额 * 留够价比例 = 留够金额
     */
    @Override
    public BigDecimal computeLeaveEnoughAmount(LeaveEnoughAmountComputeReqVO vo) {
        return vo.getLeaseContractAmount().multiply(vo.getLeaveEnoughRaio());
    }

    /**
     * 计算融资本金 融资本金 = 融资租赁合同金额 - 首付款金额
     */
    @Override
    public BigDecimal computeFinancingPrincipal(FinancingPrincipalComputeReqVO vo) {
        BigDecimal downPaymentAmount =
            vo.getDownPaymentAmount() == null ? BigDecimal.valueOf(0) : vo.getDownPaymentAmount();
        return vo.getLeaseContractAmount().subtract(downPaymentAmount);
    }

    /**
     * 根据配置查询车辆价格
     */
    @Override
    public BigDecimal queryPriceByCatalogCode(String catalogCode) {
        return vehiclePriceDubboService.queryPriceByCatalogCode(catalogCode);
    }

    /**
     * 计算月租金（含税） 月租金 = 本金*月利率*(1+月利率)^n/[(1+月利率)^n-1]
     */
    @Override
    public BigDecimal computeMonthlyRent(MonthlyRentComputeReqVO vo) {
        // 贷款期限（月） 12 36 48
        Integer termOfLoan = vo.getTermOfLoan();
        //
        //计算月利率:年利率/12
        BigDecimal yue = vo.getAnnualInterestRate().divide(new BigDecimal(12), 10, ROUND_HALF_DOWN);
        BigDecimal result1 = yue.add(new BigDecimal(1)).pow(termOfLoan).multiply(yue)
            .multiply(vo.getFinancingPrincipal());
        BigDecimal result2 = yue.add(new BigDecimal(1)).pow(termOfLoan).subtract(new BigDecimal(1));
        BigDecimal result = result1.divide(result2, 2, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    @Override
    public List<FinancialComputeColumnsRespVO> queryColumnModel(String type) {
        return financialComputeColumnsServiceImpl.queryColumnModel(type);
    }
}
